package top.college.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import top.college.dao.JDBC_ConnClose;
import top.college.dao.JDBC_SQL;
import top.college.model.AdminVO;
import top.college.screen.LoginMain;

public class AdminCreate {
	public static AdminVO av = new AdminVO(); 
	public static boolean check;
	
	public static boolean checkAdmin( JTextField AdId ) {
		Connection conn = null;
		PreparedStatement pstmt = null; 
		ResultSet rs = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.AdminSelectAll2SQL);
			pstmt.setString( 1, AdId.getText() );
			rs = pstmt.executeQuery();
			if ( rs.next() ) {
				check = false;
				return false; 
			} else { check = true; return true; }
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return false;
	}
	
	public static boolean createAdmin( JTextField AdId, JTextField AdNa, JPasswordField AdPw1, JTextField AdTe ) {
		Connection conn = null;
		PreparedStatement pstmt = null; 
		ResultSet rs = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.AdminInsertSQL);
			pstmt.setString( 1, AdId.getText() );
			pstmt.setString( 2, AdNa.getText() );
			pstmt.setString( 3, AdPw1.getText() );
			pstmt.setString( 4, AdTe.getText() );
			pstmt.executeUpdate();
			return true;
		} catch ( Exception ex ) {
			ex.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return false;
	}
}